<template>
	<el-dialog :title="dialogTitle" :visible.sync="visible" width="560px" :close-on-click-modal="false">
		<el-form ref="form" :model="formData" :rules="formRules" :label-width="!isAddSub ? '100px' : '80px'" class="config_form_wrapper">
			<el-form-item label="实验站" prop="pid" v-if="isShowPid">
				<el-select v-model="formData.pid" placeholder="请选择实验站" style="width: 100%">
					<el-option v-for="(item, index) in treeData" :key="index" :label="item.name" :value="item.id" />
				</el-select>
			</el-form-item>
			<el-form-item :label="`${formTypeName}名称`" prop="name">
				<el-input v-model="formData.name" :placeholder="`请输入${formTypeName}名称`"></el-input>
			</el-form-item>
			<el-form-item :label="`${formTypeName}描述`" prop="content">
				<el-input type="textarea" v-model="formData.content" :placeholder="`请输入${formTypeName}描述`"></el-input>
			</el-form-item>
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="formData.sort" controls-position="right"></el-input-number>
			</el-form-item>
		</el-form>
		<template slot="footer">
			<el-button size="medium" @click="visible = false">取消</el-button>
			<el-button size="medium" type="primary" @click="handleSubmitForm">{{ handleName }}</el-button>
		</template>
	</el-dialog>
</template>

<script>
import { mapGetters } from 'vuex';
import formMixins from '../formMixins';
export default {
	mixins: [formMixins],
	data() {
		return {
			defaultProps: {
				label: 'name',
				children: 'children'
			},
			formRules: {
				name: [{ required: true, message: '请输入领域名称', trigger: 'blur' }],
				content: [{ required: true, message: '请输入领域描述', trigger: 'blur' }],
				sort: [{ required: true, message: '请输入正整数序号', trigger: 'blur' }]
			}
		};
	},
	computed: {
		...mapGetters(['siteConfig']),
		handleName() {
			if (this.addOrEdit) {
				return '新增';
			} else if (this.isAddSub) {
				return '新增';
			} else {
				return '修改';
			}
		},
		formTypeName() {
			if (this.addOrEdit) {
				return '实验站';
			} else if (this.isAddSub) {
				return '房间';
			} else {
				return this.formData.pid !== '0' ? '房间' : '实验站';
			}
		},
		isShowPid() {
			return !this.isAddSub && Object.keys(this.formData).length !== 0 && this.formData.pid !== '0';
		},
		dialogTitle() {
			return `${this.handleName}${this.formTypeName}`;
		},
		treeData() {
			return this.siteConfig.filter((n) => n.type === '6' && n.pid === '0');
		}
	},
	methods: {
		//格式化表单参数
		formatFormParams() {
			let params = { ...this.formData, type: this.type };
			if (this.addOrEdit && !this.isAddSub) {
				params['pid'] = '0';
			}
			return params;
		}
	}
};
</script>

<style lang="scss" scoped>
.config_form_wrapper {
	flex: 1;
	padding: 10px 10px 0 0;
}
</style>
